﻿// <copyright file="DomainProvinceEditGM.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication
{
    using System.Collections.Generic;
    using LightSwitchApplication.UserCode;
    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Presentation.Extensions;

    public partial class DomainProvinceEditGM
    {
        partial void DomainProvinceEditGM_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            this.DomainProvince = this.DomainProvinceQuery;
            this.DisplayName = string.Format("Edit {0}", this.DomainProvince.Name);
        }

        #region Add/Edit LawHolding

        partial void LawHoldingsAddAndEditNew_Execute()
        {
            var holding = this.LawHoldings.AddNew();
            holding.Visibility = (int)VisibilityEnum.Public;

            this.FindControl("ModalLawHolding").DisplayName = string.Format("Add new Law Holding in {0}", this.DomainProvince.Name);
            this.OpenModalWindow("ModalLawHolding");
        }

        partial void LawHoldingsEditSelected_CanExecute(ref bool result)
        {
            result = this.LawHoldings.SelectedItem != null;
        }

        partial void LawHoldingsEditSelected_Execute()
        {
            this.FindControl("ModalLawHolding").DisplayName = "Edit Law Holding";
            this.OpenModalWindow("ModalLawHolding");
        }

        partial void SubmitModalLawHolding_Execute()
        {
            this.CloseModalWindow("ModalLawHolding");
        }

        partial void CancelModalLawHolding_Execute()
        {
            this.CloseModalWindow("ModalLawHolding");
            this.LawHoldings.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit TempleHolding

        partial void TempleHoldingsAddAndEditNew_Execute()
        {
            var holding = this.TempleHoldings.AddNew();
            holding.Visibility = (int)VisibilityEnum.Public;

            this.FindControl("ModalTempleHolding").DisplayName = string.Format("Add new Temple Holding in {0}", this.DomainProvince.Name);
            this.OpenModalWindow("ModalTempleHolding");
        }

        partial void TempleHoldingsEditSelected_CanExecute(ref bool result)
        {
            result = this.TempleHoldings.SelectedItem != null;
        }

        partial void TempleHoldingsEditSelected_Execute()
        {
            this.FindControl("ModalTempleHolding").DisplayName = "Edit Temple Holding";
            this.OpenModalWindow("ModalTempleHolding");
        }

        partial void SubmitModalTempleHolding_Execute()
        {
            this.CloseModalWindow("ModalTempleHolding");
        }

        partial void CancelModalTempleHolding_Execute()
        {
            this.CloseModalWindow("ModalTempleHolding");
            this.TempleHoldings.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit GuildHolding

        partial void GuildHoldingsAddAndEditNew_Execute()
        {
            var holding = this.GuildHoldings.AddNew();
            holding.Visibility = (int)VisibilityEnum.Public;

            this.FindControl("ModalGuildHolding").DisplayName = string.Format("Add new Guild Holding in {0}", this.DomainProvince.Name);
            this.OpenModalWindow("ModalGuildHolding");
        }

        partial void GuildHoldingsEditSelected_CanExecute(ref bool result)
        {
            result = this.GuildHoldings.SelectedItem != null;
        }

        partial void GuildHoldingsEditSelected_Execute()
        {
            this.FindControl("ModalGuildHolding").DisplayName = "Edit Guild Holding";
            this.OpenModalWindow("ModalGuildHolding");
        }

        partial void SubmitModalGuildHolding_Execute()
        {
            this.CloseModalWindow("ModalGuildHolding");
        }

        partial void CancelModalGuildHolding_Execute()
        {
            this.CloseModalWindow("ModalGuildHolding");
            this.GuildHoldings.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit TradeRoute

        partial void TradeRoutesInDomainProvinceAddAndEditNew_Execute()
        {
            var route = this.TradeRoutesInDomainProvince.AddNew();
            route.Visibility = (int)VisibilityEnum.Public;
            route.Type = (int)TradeRouteTypeEnum.Land;

            this.FindControl("ModalWindowTradeRouteStart").DisplayName = string.Format("Add new Trade Route in {0}", this.DomainProvince.Name);
            this.OpenModalWindow("ModalWindowTradeRouteStart");
        }

        partial void TradeRoutesInDomainProvinceEditSelected_CanExecute(ref bool result)
        {
            result = this.TradeRoutesInDomainProvince.SelectedItem != null;
        }

        partial void TradeRoutesInDomainProvinceEditSelected_Execute()
        {
            var tr = this.TradeRoutesInDomainProvince.SelectedItem;
            if (tr.StartPoint.Province.Id == this.DomainProvince.Id)
            {
                this.FindControl("ModalWindowTradeRouteStart").DisplayName = "Edit trade route";
                this.OpenModalWindow("ModalWindowTradeRouteStart");
            }
            else
            {
                this.FindControl("ModalWindowTradeRouteEnd").DisplayName = "Edit trade route";
                this.OpenModalWindow("ModalWindowTradeRouteEnd");
            }
        }

        partial void SubmitModalWindowTradeRoute_Execute()
        {
            this.CloseModalWindow("ModalWindowTradeRouteStart");
            this.CloseModalWindow("ModalWindowTradeRouteEnd");
        }

        partial void CancelModalWindowTradeRoute_Execute()
        {
            this.CloseModalWindow("ModalWindowTradeRouteStart");
            this.CloseModalWindow("ModalWindowTradeRouteEnd");
            this.TradeRoutesInDomainProvince.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit SourceHodling

        partial void SourceHoldingsAddAndEditNew_Execute()
        {
            var holding = this.SourceHoldings.AddNew();
            holding.Visibility = (int)VisibilityEnum.Public;

            this.FindControl("ModalSourceHolding").DisplayName = string.Format("Add new Source Holding in {0}", this.DomainProvince.Name);
            this.OpenModalWindow("ModalSourceHolding");
        }

        partial void SourceHoldingsEditSelected_CanExecute(ref bool result)
        {
            result = this.SourceHoldings.SelectedItem != null;
        }

        partial void SourceHoldingsEditSelected_Execute()
        {
            this.FindControl("ModalSourceHolding").DisplayName = "Edit Source Holding";
            this.OpenModalWindow("ModalSourceHolding");
        }

        partial void SubmitModalSourceHolding_Execute()
        {
            this.CloseModalWindow("ModalSourceHolding");
        }

        partial void CancelModalSourceHolding_Execute()
        {
            this.CloseModalWindow("ModalSourceHolding");
            this.SourceHoldings.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit LeyLine

        partial void LeyLinesInDomainProvinceAddAndEditNew_Execute()
        {
            var line = this.LeyLinesInDomainProvince.AddNew();
            line.Visibility = (int)VisibilityEnum.Public;

            this.FindControl("ModalWindowLeyLineStart").DisplayName = string.Format("Add new Ley Line in {0}", this.DomainProvince.Name);
            this.OpenModalWindow("ModalWindowLeyLineStart");
        }

        partial void LeyLinesInDomainProvinceEditSelected_CanExecute(ref bool result)
        {
            result = this.LeyLinesInDomainProvince.SelectedItem != null;
        }

        partial void LeyLinesInDomainProvinceEditSelected_Execute()
        {
            var ll = this.LeyLinesInDomainProvince.SelectedItem;
            if (ll.StartPoint.Province.Id == this.DomainProvince.Id)
            {
                this.FindControl("ModalWindowLeyLineStart").DisplayName = "Edit ley line";
                this.OpenModalWindow("ModalWindowLeyLineStart");
            }
            else
            {
                this.FindControl("ModalWindowLeyLineEnd").DisplayName = "Edit ley line";
                this.OpenModalWindow("ModalWindowLeyLineEnd");
            }
        }

        partial void SubmitModalWindowLeyLine_Execute()
        {
            this.CloseModalWindow("ModalWindowLeyLineStart");
            this.CloseModalWindow("ModalWindowLeyLineEnd");
        }

        partial void CancelModalWindowLeyLine_Execute()
        {
            this.CloseModalWindow("ModalWindowLeyLineStart");
            this.CloseModalWindow("ModalWindowLeyLineEnd");
            this.LeyLinesInDomainProvince.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit Army

        partial void ArmiesAddAndEditNew_Execute()
        {
            this.Application.ShowArmyAddEditGM(null);
        }

        partial void ArmiesEditSelected_CanExecute(ref bool result)
        {
            result = this.Armies.SelectedItem != null;
        }

        partial void ArmiesEditSelected_Execute()
        {
            this.Application.ShowArmyAddEditGM(this.Armies.SelectedItem.Id);
        }

        #endregion
    }
}